package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmLoginDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.BCrypt;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    @Override
    public ResponseResult login(WmLoginDto dto) {
        WmUser wmUser = this.lambdaQuery()
                .eq(WmUser::getName, dto.getName())
                .one();
        if(wmUser == null){
            ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        boolean checkpw = BCrypt.checkpw(dto.getPassword(), wmUser.getPassword());
        if(!checkpw){
            ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        if(wmUser.getStatus() != 9){
            ResponseResult.errorResult(AppHttpCodeEnum.INVALID_STATUS);
        }
        long Id = wmUser.getId().longValue();
        String token = AppJwtUtil.getToken(Id);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("user",wmUser);
        hashMap.put("token",token);
        return ResponseResult.okResult(hashMap);
    }
}